package com.cotte.flow.hbase.dao;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhuang on 2019/1/3.
 */
@Component
public class BrushcardWorkhoursDaoImpl {

    private String tableName = "step:brushcard_workhours";
    private Table table = null;

    @Autowired
    private Connection connection;

    public ArrayList getData(String filter){
        Scan s = new Scan();
        s.setFilter(new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(filter)));
        //
        ArrayList result = new ArrayList();
        ResultScanner scanner = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            scanner = table.getScanner(s);
            for(Result row:scanner){
                Map<String,String> one = new HashMap<String,String>();
                one.put("key", Bytes.toString(row.getRow()));
                one.put("fabric", Bytes.toString(row.getValue("info".getBytes(),"fabric".getBytes())));
                one.put("gyzh",Bytes.toString(row.getValue("info".getBytes(),"gyzh".getBytes())));
                one.put("odid",Bytes.toString(row.getValue("info".getBytes(),"odid".getBytes())));
                one.put("stepcode",Bytes.toString(row.getValue("info".getBytes(),"stepcode".getBytes())));
                one.put("syscode",Bytes.toString(row.getValue("info".getBytes(),"syscode".getBytes())));
                result.add(one);
            }
            scanner.close();
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public ArrayList getTest(String filter){
        ArrayList result = new ArrayList();
        Map<String,String> one = new HashMap<String,String>();
        one.put("key", "xxx");
        one.put("fabrictype", "条纹");
        one.put("gyzh", "05C2,04N1");
        one.put("odid", "xxx");
        one.put("stepcode", "101");
        one.put("syscode", "xxx");
        one.put("time", "37");
        one.put("loaddate", "180102");
        result.add(one);
        return result;
    }
}
